package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaBudgetDao;
import com.qymerge.manage.base.dao.IMetaBudgetProductDao;
import com.qymerge.manage.base.ro.MetaBudgetRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *<p>Title: MetaBudgetDaoImpl</p>
 *<p>Description: 组织架构</p>
 *
 * @author Xiao
 * @date 2025/5/2 01:23
 * @version 1.0.0
 */
@Repository
public class MetaBudgetDaoImpl extends BaseDao implements IMetaBudgetDao {

//    private String dwd_MCRS_BDM_Budget = "SELECT Id,OrgId,CompanyId,CompanyName,CompanyCode,CostCenterId,PeriodYear,VerionNumber,Actived FROM dwd_MCRS_BDM_Budget where 1=1";
    private String dwd_MCRS_BDM_Budget = "SELECT b.*, o.Name as OrgName, c.Code as InternalCompanyCode, c.Name as InternalCompanyName, cc.Code as CostCenterCode, cc.Name as CostCenterName " +
        "FROM dwd_MCRS_BDM_Budget b " +
        "    left join dwd_MCRS_MDM_meta_Org o on b.OrgId = o.Id" +
        "    left join dwd_MCRS_MDM_meta_Company c on b.InternalCompanyId = c.Id" +
        "    left join dwd_MCRS_MDM_meta_CostCenter cc on b.CostCenterId = cc.Id where 1=1";

    @Override
    public List list(MetaBudgetRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_BDM_Budget;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if(StringUtils.isNotBlank(ro.getCompanyId())){
            sql += " AND b.CompanyId = :CompanyId ";
            paramMap.put("CompanyId", ro.getCompanyId());
        }

        if(StringUtils.isNotBlank(ro.getCompanyCode())){
            sql += " AND b.CompanyCode LIKE :CompanyCode ";
            paramMap.put("CompanyCode", "%"+ro.getCompanyCode()+"%");
        }

        if(StringUtils.isNotBlank(ro.getCompanyName())){
            sql += " AND b.CompanyName LIKE :CompanyName ";
            paramMap.put("CompanyName", "%" + ro.getCompanyName() + "%");
        }
        
        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaBudgetRO>(MetaBudgetRO.class));
    }

    @Override
    public int total(MetaBudgetRO ro) {
        String sql = "select count(1) from dwd_MCRS_BDM_Budget where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if(StringUtils.isNotBlank(ro.getCompanyId())){
            sql += " AND CompanyId = :CompanyId ";
            paramMap.put("CompanyId", ro.getCompanyId());
        }

        if(StringUtils.isNotBlank(ro.getCompanyCode())){
            sql += " AND CompanyCode LIKE :CompanyCode ";
            paramMap.put("CompanyCode", "%"+ro.getCompanyCode()+"%");
        }

        if(StringUtils.isNotBlank(ro.getCompanyName())){
            sql += " AND CompanyName LIKE :CompanyName ";
            paramMap.put("CompanyName", "%"+ro.getCompanyName()+"%");
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public List<MetaBudgetRO> findAll(MetaBudgetRO ro) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_BDM_Budget);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        
        sql.append(" order by sort desc");
        return getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new BeanPropertyRowMapper<MetaBudgetRO>(MetaBudgetRO.class));
    }

    @Override
    public MetaBudgetRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_BDM_Budget);
        sql.append(" and b.Id=? ");
        RowMapper<MetaBudgetRO> rowMapper = new BeanPropertyRowMapper<MetaBudgetRO>(MetaBudgetRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public int insert(MetaBudgetRO ro) {
        StringBuilder sql = new StringBuilder("insert into dwd_MCRS_BDM_Budget(OrgId,CompanyId,CompanyName,CompanyCode,CostCenterId,InternalCompanyId,PeriodYear,VerionNumber,Actived,create_user_id,create_user_name,create_time) values(?,?,?,?,?,?,?,?,?,?,?,?)");

        List params = new ArrayList();
        params.add(ro.getOrgId());
        params.add(ro.getCompanyId());
        params.add(ro.getCompanyName());
        params.add(ro.getCompanyCode());
        params.add(ro.getCostCenterId());
        params.add(ro.getInternalCompanyId());
        params.add(ro.getPeriodYear());
        params.add(ro.getVerionNumber());
        params.add(ro.getActived());
        params.add(ro.getCreate_user_id());
        params.add(ro.getCreate_user_name());
        params.add(ro.getCreate_time());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int update(MetaBudgetRO ro) {
        StringBuilder sql = new StringBuilder("update dwd_MCRS_BDM_Budget set OrgId=:OrgId,CompanyId=:CompanyId,CompanyName=:CompanyName,CompanyCode=:CompanyCode,CostCenterId=:CostCenterId,InternalCompanyId=:InternalCompanyId,PeriodYear=:PeriodYear,VerionNumber=:VerionNumber,Actived=:Actived where Id=:Id ");

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("OrgId", ro.getOrgId());
        paramsMap.put("CompanyId", ro.getCompanyId());
        paramsMap.put("CompanyName", ro.getCompanyName());
        paramsMap.put("CompanyCode", ro.getCompanyCode());
        paramsMap.put("CostCenterId", ro.getCostCenterId());
        paramsMap.put("InternalCompanyId", ro.getInternalCompanyId());
        paramsMap.put("PeriodYear", ro.getPeriodYear());
        paramsMap.put("VerionNumber", ro.getVerionNumber());
        paramsMap.put("Actived", ro.getActived());

        paramsMap.put("Id", ro.getId());
        return getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
    }

    @Override
    public int remove(long Id) {
        // 删除
        String sql="delete from dwd_MCRS_BDM_Budget where Id=?";
        return getJdbcTemplate().update(sql, Id);
    }


}
